package yannan.voiplock4ble.adapter.base;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import yannan.voiplock4ble.BuildConfig;
import yannan.voiplock4ble.R;

/**
 * 说明：底部自动加载的基础Adapter
 * 作者：mainTel
 * 时间：2016/11/19 16:59
 * 备注：
 */
public abstract class BaseAutoLoadingAdapter<T> extends RecyclerViewBaseAdapter<T> {

    public enum FootStatue {
        LOADING,
        ERROR,
        OVER
    }

    static final int TYPE_FOOTER = 0;
    static final int TYPE_ITEM = 1;

    RecyclerView recyclerView;
    boolean isDefaultFoot = false;
    boolean isLoadAllow = true;
    boolean isLoading = false;
    FootStatue footStatue = FootStatue.LOADING;

    public BaseAutoLoadingAdapter(List<T> list, Context ctx, RecyclerView recyclerView) {
        super(list, ctx);
        this.recyclerView = recyclerView;
        recyclerView.addOnScrollListener(onScrollListener);
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 0 : list.size() + 1;
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        int id = 0;
        if (viewType == TYPE_FOOTER) {
            id = createFootView();
            if (id == 0) {
                isDefaultFoot = true;
                id = R.layout.item_auto_load;
            }
        } else {
            id = createItemView();
        }
        return id;
    }

    /**
     * 创建底部布局 返回0 使用默认布局
     *
     * @return id
     */
    public abstract int createFootView();

    /**
     * 创建Item布局
     *
     * @return id
     */
    public abstract int createItemView();

    @Override
    public void onBindViewHolder(MyViewHolder holder, int poi) {
        if (poi < list.size()) { // 说明是一般布局
            onItemBindViewHolder(holder, poi);
        } else { //说明是底部底部加载布局
            if (isDefaultFoot) {
                switch (footStatue) {
                    case LOADING:
                        holder.get(R.id.ll_loading).setVisibility(View.VISIBLE);
                        holder.get(R.id.ll_loading_error).setVisibility(View.GONE);
                        holder.get(R.id.ll_loading_over).setVisibility(View.GONE);
                        break;
                    case ERROR:
                        holder.get(R.id.ll_loading_error).setVisibility(View.VISIBLE);
                        holder.get(R.id.ll_loading).setVisibility(View.GONE);
                        holder.get(R.id.ll_loading_over).setVisibility(View.GONE);
                        break;
                    case OVER:
                        holder.get(R.id.ll_loading).setVisibility(View.GONE);
                        holder.get(R.id.ll_loading_over).setVisibility(View.VISIBLE);
                        holder.get(R.id.ll_loading_error).setVisibility(View.GONE);
                        break;
                }
                holder.get(R.id.tv_reload).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        footStatue = FootStatue.LOADING;
                        isLoading = true;
                        notifyDataSetChanged();
                        if (null != onRecyclerLoading) {
                            onRecyclerLoading.onRecyclerLoading(true);
                        }
                    }
                });
            } else {
                onFootBindViewHolder(holder, poi);
            }
        }
    }


    public abstract void onItemBindViewHolder(MyViewHolder holder, int poi);

    public abstract void onFootBindViewHolder(MyViewHolder holder, int poi);

    @Override
    public void upDate(List<T> list) {
        isLoadAllow = true;
        isLoading = false;
        footStatue = FootStatue.LOADING;
        super.upDate(list);
    }

    /**
     * 加载完成
     */
    public void loadComplete() {
        isLoading = false;
        footStatue = FootStatue.LOADING;
        notifyItemRemoved(getItemCount());
    }

    /**
     * 没有更多数据
     */
    public void loadNoMore() {
        isLoading = true;
        footStatue = FootStatue.OVER;
        notifyDataSetChanged();
    }

    /**
     * 加载错误
     */
    public void loadError() {
        isLoading = true;
        footStatue = FootStatue.ERROR;
        notifyDataSetChanged();
    }

    /**
     * 禁止自动加载
     */
    public void setAutoLoadAllow(boolean b) {
        isLoadAllow = b;
    }

    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (myOnScrollListener != null) {
                myOnScrollListener.onScrollStateChanged(recyclerView, newState);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (myOnScrollListener != null) {
                myOnScrollListener.onScrolled(recyclerView, dx, dy);
            }
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int lastItem = layoutManager.findLastVisibleItemPosition();
            if (lastItem + 1 == getItemCount()) {
                if (!isLoadAllow) { // 不允许自动加载
                    notifyItemRemoved(getItemCount());
                    return;
                }
                if (!isLoading) {
                    isLoading = true;
                    if (onRecyclerLoading != null) {
                        if (BuildConfig.DEBUG) Log.d("LockRecordFragment", "自动更新");
                        onRecyclerLoading.onRecyclerLoading(false);
                    }
                }
            }
        }
    };

    /**
     * 设置加载更多的监听
     *
     * @param onRecyclerLoading
     */
    public void setOnRecyclerLoading(IOnRecyclerLoading onRecyclerLoading) {
        this.onRecyclerLoading = onRecyclerLoading;
    }

    private IOnRecyclerLoading onRecyclerLoading;

    /**
     * 设置滑动监听
     *
     * @param myOnScrollListener
     */
    public void setMyOnScrollListener(IMyOnScrollListener myOnScrollListener) {
        this.myOnScrollListener = myOnScrollListener;
    }

    private IMyOnScrollListener myOnScrollListener;

}
